package next.apps.biz;

import next.apps.bean.OrderInfo;
import next.apps.dao.OrderDao;
import next.apps.service.OrderService;
import next.apps.util.UniqueId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.NoSuchAlgorithmException;
import java.util.List;

@Service
public class OrderBizImpl implements OrderBiz {
    @Autowired
    private OrderDao orderDao;
    @Override
    @Transactional
    public int save(List<OrderInfo> orderInfoList,String orderId) {
        if(orderId == null || orderId.isEmpty()){
            try {
                orderId = UniqueId.generateID();
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
        }
        String finalId = orderId;
        orderInfoList.forEach(item ->{
            item.setId(finalId);
        });
        return orderDao.save(orderInfoList);
    }
}
